﻿using System;
using System.Data;
using System.Windows.Forms;
using RemoteTestRunner.BusinessLogic;
using RemoteTestRunner.ServiceFacade;
using RemoteTestRunner.BusinessLogic.Views;
using RemoteTestRunner.Contracts;

namespace RemoteTestRunner.FormsUI
{
    public partial class ProjectAdmin : Form, IEditProjectsView
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectAdmin"/> class.
        /// </summary>
        public ProjectAdmin()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Initializes the form.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ProjectAdmin_Load(object sender, EventArgs e)
        {
            grdProjects.DataSource = Model.GetTestData();
            grdProjects.DataMember = "Project";

            // Sort by Name
            grdProjects.Sort(grdProjects.Columns["Name"], System.ComponentModel.ListSortDirection.Ascending);

            // Hide all columns except for "Name"
            foreach (DataGridViewColumn column in grdProjects.Columns)
            {
                column.Visible = (column.Name == "Name");
            }

            UpdateOptions();
        }

        /// <summary>
        /// Set default values for new rows as they are created.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellCancelEventArgs"/> instance containing the event data.</param>
        private void grdProjects_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (Convert.IsDBNull(grdProjects["Name", e.RowIndex].Value))
            {
                grdProjects.EndEdit();
            }
            else
            {
                // Set default vaules
                if (Convert.IsDBNull(grdProjects["CreatedBy", e.RowIndex].Value))
                {
                    grdProjects["CreatedBy", e.RowIndex].Value = Environment.UserName;
                    grdProjects["CreatedDate", e.RowIndex].Value = DateTime.Now;
                }

                grdProjects["ModifiedBy", e.RowIndex].Value = Environment.UserName;
                grdProjects["ModifiedDate", e.RowIndex].Value = DateTime.Now;
            }
        }

        /// <summary>
        /// Deletes the selected project.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grdProjects.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in grdProjects.SelectedRows)
                {
                    if (!row.IsNewRow)
                    {
                        grdProjects.Rows.Remove(row);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select a row to delete.");
            }
        }

        /// <summary>
        /// Enable the Delete button if a row has been clicked.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void grdProjects_Click(object sender, EventArgs e)
        {
            UpdateOptions();
        }

        /// <summary>
        /// Saves the current changes.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            Model.SaveTestData((DataSet)grdProjects.DataSource);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Updates the options based on the current state of the dialog.
        /// </summary>
        private void UpdateOptions()
        {
            btnDelete.Enabled = (grdProjects.SelectedRows.Count > 0);
        }

        #endregion

        #region IEditProjectsView Members

        public IRTRService Model { get; set; }

        #endregion
    }
}
